package com.power.sifa.netlogin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.approve.model.Apl_arrive_material;
import com.power.sifa.approve.model.pageInfo.Apl_arrive_personPageInfo;
import com.power.sifa.approve.service.Apl_arrive_materialService;
import com.power.sifa.approve.service.Apl_arrive_personService;
import com.power.sifa.netlogin.service.Net_userCommonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author xiongzhiwen
 * @version 1.0
 * @description: TODO
 * @date 2024/4/25 10:53
 */
@Service
@RequiredArgsConstructor
public class Net_userCommonServiceImpl extends BaseServiceImpl implements Net_userCommonService {


    private final Apl_arrive_personService apl_arrive_personService;

    private final Apl_arrive_materialService apl_arrive_materialService;

    @Override
    public PageDataResponse getArrivePage(Apl_arrive_personPageInfo pageinfo) {
        List<Map<String,Object>> arriveList = apl_arrive_personService.findArriveListByPhone(pageinfo);
        // 分页
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(arriveList)) {
            // 查询对应的送达文件
            arriveList.stream().forEach(el -> {
                String recordId = el.get("recordid").toString();
                // 查询对应的送达文件材料
                List<Apl_arrive_material> materialList = apl_arrive_materialService.findMaterialsByRecordIds(Collections.singletonList(recordId));
                el.put("files", materialList);
            });

            Long total = Long.valueOf(pageinfo.getAllcount());
            int size = pageinfo.getPageSize() * pageinfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(pageinfo.getPageNumber() + 1);
            }
            page.setSize(pageinfo.getPageSize());
            page.setCurrent(pageinfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(arriveList);
        return page;
    }

}
